<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">

  </forms>
</template>

<script>
import s94Mjs from "s94-js";
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import {_href, param_encode} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = {
      '导出_primary':()=>{
        this.onsubmit();
      }
    };
    let forms_config = {
      _table_list_: {name:'', label:'导出的表', type:'checkbox', config:[], value:'', tips:''}
    }

    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,

      table_map: {},
    }
  },
  methods: {
    onsubmit(formData){
      let param = {};
      Object.values(this.config).forEach((item)=>{
        if (!item.name || item.remove) return;
        param[item.name] = item.value || 'all';
      })
      param = param_encode(param);
      let url = `/admin/db_data/export?${param}`;
      window.open(_href(url), '_blank');
    }
  },
  watch: {
    'config._table_list_.value'(val, oldVal){
      let showTables = val?val.split(','):[];
      Object.keys(this.config).forEach(key=>{
        let row = this.config[key];
        if (row.name.indexOf('select_map[') !== 0) return false;
        this.config[key].remove = !~showTables.indexOf(key);
      })
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
    }

    //其他初始化操作
    ajax('/admin/db_data/config_map', ({data})=>{
      data.table_list.forEach(row=>{
        this.config[row.value] = {name:`select_map[${row.value}]`, label:row.content, type:'list_ids', config:row.list_url, tips:'为空表示所有项目', remove:true};
      })
      this.config._table_list_.config = data.table_list;

    })
  }
}
</script>
